//
//  CommonTools.m
//  ShenTongECar
//
//  Created by 正合适 on 2017/7/19.
//  Copyright © 2017年 正合适. All rights reserved.
//

#import "CommonTools.h"

@implementation CommonTools
#pragma mark - 调用系统拨打电话
+ (void)callWithPhone:(NSString *)phone {
    UIWebView * callWebview = [[UIWebView alloc]init];
    [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",phone]]]];
    [[UIApplication sharedApplication].keyWindow addSubview:callWebview];
}

#pragma mark - 字典转json字符串
+ (NSString*)dictionaryToJson:(NSDictionary *)dic {
    if (dic) {
        NSError *parseError = nil;
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];
        return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    } else {
        return nil;
    }
}

#pragma mark - json字符串转字典
+ (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString {
    if (jsonString == nil) {
        return nil;
    }
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers
                                                          error:&err];
    if (err){
        NSLog(@"json解析失败：%@",err);
        return nil;
    }
    return dic;
}
@end
